package com.softeem.yuedong_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.yuedong_server.dto.Result;
import com.softeem.yuedong_server.entity.Music;
import com.softeem.yuedong_server.entity.Musiclist;
import com.softeem.yuedong_server.service.MusicService;
import com.softeem.yuedong_server.service.MusiclistService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/musiclist")
@Tag(name = "歌单管理")
public class MusiclistController extends BaseController{

    @Resource
    private MusiclistService musiclistService;

    @Resource
    private MusicService musicService;

    /*歌单添加*/
    @Operation(summary = "歌单添加",description = "添加歌单信息，需要提供基本的参数，例如名称和简介")
    @PostMapping("/")
    public Result addMusiclist(@RequestBody Musiclist musiclist) {
        return musiclistService.save(musiclist)  ? Result.ok().setMsg("添加成功").setData(musiclist) : Result.fail().setMsg("添加失败");
    }

    /*歌单删除*/
    @Operation(summary = "歌单删除",description = "根据提供的歌单id删除歌单，id必须提供")
    @DeleteMapping("/{id}")
    public Result delMusiclist(@PathVariable Integer id) {
        return musiclistService.removeById(id) ? Result.ok().setMsg("删除成功") : Result.fail().setMsg("删除失败");
    }

    /*歌单更新*/
    @Operation(summary = "歌单更新",description = "修改歌单信息，id必须提供")
    @PutMapping("/")
    public Result updateMusiclist(@RequestBody Musiclist musiclist) {
        return musiclistService.updateById(musiclist) ? Result.ok().setMsg("更新成功").setData(musiclist) : Result.fail().setMsg("更新失败");
    }

    /*歌单详情*/
    @Operation(summary = "歌单详情",description = "必须提供id，查看歌单详情，并显示歌单所有的歌曲信息")
    @GetMapping("/{id}")
    public Result findMusiclist(@PathVariable Integer id) {
        Musiclist musiclist = musiclistService.getById(id);
        if(Objects.nonNull(musiclist)){
            Music music = musicService.getById(musiclist.getUid());
            return Result.ok().setData(musiclist).setData(music);
        }
        return Result.fail().setMsg("歌单不存在");
    }
    /*歌单列表*/
    @Operation(summary = "歌单列表",description = "查询所有的歌单信息，提供页码和每页大小，分页显示数据")
    @Parameters({
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "每页大小",required = true)
    })
    @GetMapping("/list")
    public Result findAllMusiclist(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit) {
        List<Musiclist> musiclists = musiclistService.list(new Page<>(page, limit));
        long count = musiclistService.count();
        //返回结果
        return Result.ok().setData(musiclists).setCount(count);
    }
}
